package com.dao.user;

import com.dao.BaseDao;
import com.pojo.Doctor;
import com.pojo.User;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImp implements UserDao{

    @Override
    public void appointment(Connection connection, String Ac,String ke,String date) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet res = null;
        Doctor doc = null;
        int update;
        if(connection != null){
            String sql = "select * from doctor where ke=?";
            Object[] params = {ke};
            res = BaseDao.execute(connection,sql,params,res,pstm);
            if(res.next()) {
                doc = new Doctor();
                doc.setID(res.getString("ID"));
                doc.setName(res.getString("Name"));
                doc.setKe(res.getString("Ke"));
            }
            pstm = null;
            sql = "insert into yuyue (Ac,ID,Date) values (?,?,?)";
            Object[] params1 = {Ac,doc.getID(),date};
            update = BaseDao.execute(connection,sql,params1,pstm);
            BaseDao.closeResource(null,res,pstm);

        }
    }

    @Override
    public void setResUser(Connection connection, String Ac, String Psw, String Name, String Sex, String Birth) throws SQLException {
        PreparedStatement pstm = null;
        int update;
        if(connection != null) {
            String sql = "insert into user (Ac,Psw,Name,Sex,Birth) values (?,?,?,?,?)";
            Object[] params = {Ac,Psw,Name,Sex,Birth};
            update = BaseDao.execute(connection,sql,params,pstm);
            BaseDao.closeResource(null,null,pstm);
        }
    }

    @Override
    public User getLoginUser(Connection connection, String Ac) throws SQLException {
        PreparedStatement pstm = null;
        ResultSet res = null;
        User user = null;

        if(connection != null){
            String sql = "select * from user where Ac=?";
            Object[] params = {Ac};
            res = BaseDao.execute(connection,sql,params,res,pstm);
            if(res.next()) {
                user = new User();
                user.setAc(res.getString("Ac"));
                user.setPsw(res.getString("Psw"));
                user.setName(res.getString("Name"));
                user.setSex(res.getString("Sex"));
                user.setDate(res.getString("Birth"));
            }
            BaseDao.closeResource(null,res,pstm);

        }
        return user;
    }
}
